//
//  DataHandle.m
//  RedManStock
//
//  Created by lanou3g on 15/7/31.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "DataHandle.h"
#import "User.h"
static DataHandle *dataHandle=nil;
@implementation DataHandle

+ (DataHandle *)shareHandle{
    if (dataHandle==nil) {
        dataHandle=[[DataHandle alloc]init];
    }
    return dataHandle;
}

#pragma mark - 股票的plist文件 -
- (void)initStockInfoPlist{
    
    NSString *documentPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSLog(@"%@",documentPath);
    NSString *newPath=[documentPath stringByAppendingPathComponent:@"stockCollect.plist"];
    NSFileManager *fileManager=[NSFileManager defaultManager];
    //建文件夹，如果有了就不用建了
    if (![fileManager fileExistsAtPath:newPath]){
        
        NSString *filePath=[[NSBundle mainBundle] pathForResource:@"StockInfo" ofType:@"plist"];
        NSDictionary *outDic=[NSDictionary dictionaryWithContentsOfFile:filePath];
        NSArray *array=[outDic objectForKey:@"RECORDS"];
        NSMutableArray *myArray=[NSMutableArray array];
        for (NSDictionary *dic in array) {
            NSMutableDictionary *myDic=[NSMutableDictionary dictionaryWithDictionary:dic];
            [myDic setObject:@"false" forKey:@"collected"];
            [myArray addObject:myDic];
        }
        //    NSLog(@"myarray=%@",myArray);
        [myArray writeToFile:newPath atomically:YES];
    }
}

- (NSMutableArray *)allStockArray{
    if (_allStockArray==nil || _allStockArray.count==0) {
        _allStockArray=[NSMutableArray array];
    }
    //一定要remove掉
    [_allStockArray removeAllObjects];
    NSArray *array=[NSArray arrayWithContentsOfFile:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"stockCollect.plist"]];
    
    for (NSDictionary *dic in array) {
        SimpleStock *stock=[[SimpleStock alloc]init];
        
        stock.name = [dic objectForKey:@"name"];
        stock.code = [dic objectForKey:@"code"];
        NSString *str=[dic objectForKey:@"collected"];
        if ([str isEqualToString:@"false"]) {
            stock.collect=NO;
        }
        else{
            stock.collect=YES;
        }
        stock.stockStr = [stock.name stringByAppendingString:[NSString stringWithFormat:@"(%@)",stock.code]];
        [_allStockArray addObject:stock];
    }
    return _allStockArray;
}

- (void)changeMyStockWithStockCode:(NSString *)stockCode
                     add:(BOOL)add{
    
    NSString *fileStr=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"stockCollect.plist"];
    NSArray *array=[NSArray arrayWithContentsOfFile:fileStr];
    for (NSDictionary *dic in array) {
        NSString *codeStr=[dic objectForKey:@"code"];
        if ([codeStr isEqualToString:stockCode]) {
            //收藏
            if (add==YES) {
                [dic setValue:@"true" forKey:@"collected"];
            }
            //删除收藏
            else{
                [dic setValue:@"false" forKey:@"collected"];
            }
            break;
        }
    }
    //再写入
    [array writeToFile:fileStr atomically:YES];
}

#pragma mark - User的plist文件 -
- (void)initUserInfoPlist{
    
    NSString *documentPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSLog(@"%@",documentPath);
    NSString *newPath=[documentPath stringByAppendingPathComponent:@"user.plist"];
    NSFileManager *fileManager=[NSFileManager defaultManager];
    //建文件夹，如果有了就不用建了
    if (![fileManager fileExistsAtPath:newPath]){
        User *user=[[User alloc]init];
        user.totalAsset=[NSString stringWithFormat:@"1000000.00"];
        user.availableAsset=[NSString stringWithFormat:@"1000000.00"];
        user.totalPrice=[NSString stringWithFormat:@"0"];
        user.totalYield=[NSString stringWithFormat:@"0"];
        user.day_yield=[NSString stringWithFormat:@"0"];
        user.day_yieldRate=[NSString stringWithFormat:@"0"];
        user.lastTotalAsset=[NSString stringWithFormat:@"1000000.00"];
        
        [NSKeyedArchiver archiveRootObject:user toFile:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"user.plist"]];
        
    }
}


@end
